package ltd.dujiabao.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import ltd.dujiabao.eduservice.entity.EduChapter;
import ltd.dujiabao.eduservice.entity.EduCourse;
import ltd.dujiabao.eduservice.entity.EduCourseDescription;
import ltd.dujiabao.eduservice.entity.EduSubject;
import ltd.dujiabao.eduservice.entity.course.CourseStatus;
import ltd.dujiabao.eduservice.entity.vo.CourseInfoForm;
import ltd.dujiabao.eduservice.entity.vo.CoursePublishVo;
import ltd.dujiabao.eduservice.entity.vo.CourseQuery;
import ltd.dujiabao.eduservice.mapper.EduCourseMapper;
import ltd.dujiabao.eduservice.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author dujiabao
 * @since 2021-04-21
 */
@Slf4j
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
  @Autowired
  EduCourseDescriptionService eduCourseDescriptionService;
  @Autowired
  EduChapterService eduChapterService;
  @Autowired
  EduVideoService eduVideoService;

  @Override
  public boolean saveCourseInfo(CourseInfoForm courseInfoForm) {
    EduCourse course = new EduCourse();
    EduCourseDescription description = new EduCourseDescription();

    // 保存课程
    BeanUtils.copyProperties(courseInfoForm, course);
    boolean isSuccess = save(course);
    if (!isSuccess) return false;  // 如果保存失败，那就干脆直接返回吧

    // 保存课程描述
    BeanUtils.copyProperties(courseInfoForm, description);
    description.setId(course.getId()); // 保证课程表的id和描述表的id完全一样
    isSuccess = eduCourseDescriptionService.save(description);// 保存
    if (!isSuccess) return false;  // 如果保存失败，那就干脆直接返回吧

    courseInfoForm.setId(course.getId());  // 将在数据库中的id，返回到原来的表单对象中，让该id可以返回
    return true;  // 操作完成，可以返回true了
  }

  @Override
  public boolean updateCourseInfo(CourseInfoForm courseInfoForm) {
    String id = courseInfoForm.getId();
    EduCourse course = getById(id);
    boolean isSuccess; // 成功标志位
    // 如果为空，则添加，如果不为空，则修改
    if (course != null) {
      BeanUtils.copyProperties(courseInfoForm, course);
      isSuccess = updateById(course);
    } else {
      course = new EduCourse();
      log.warn("id: " + id + "的 EduCourse 在数据库中不存在，已重新创建对象");
      BeanUtils.copyProperties(courseInfoForm, course);
      isSuccess = save(course);
    }

    if (!isSuccess) {
      log.error("保存课程失败，Course：" + course.toString());
      return false;  // 如果保存失败，那就干脆直接返回吧
    }

    EduCourseDescription description = eduCourseDescriptionService.getById(id);
    if (description != null) {
      BeanUtils.copyProperties(courseInfoForm, description);
      isSuccess = eduCourseDescriptionService.updateById(description);
    } else {
      description = new EduCourseDescription();
      log.warn("id: " + id + "的 EduCourseDescription 在数据库中不存在，已重新创建对象");
      BeanUtils.copyProperties(courseInfoForm, description);
      isSuccess = eduCourseDescriptionService.save(description);
    }

    if (!isSuccess) {
      log.error("保存课程描述失败，CourseDescription：" + course.toString());
      return false;  // 如果保存失败，那就干脆直接返回吧
    }

    return isSuccess;  // 返回是否更新成功
  }

  @Override
  public CourseInfoForm getCourseInfo(String id) {
    // 查询信息
    EduCourse course = getById(id);
    EduCourseDescription description = eduCourseDescriptionService.getById(id);
    // 将信息包装成courseInfoForm
    CourseInfoForm courseInfoForm = new CourseInfoForm();
    if (course != null) BeanUtils.copyProperties(course, courseInfoForm);
    if (description != null) BeanUtils.copyProperties(description, courseInfoForm);
    return courseInfoForm;
  }

  @Override
  public IPage<EduCourse> pageQuery(Page<EduCourse> page, CourseQuery courseQuery) {
    if (courseQuery == null) {
      return page(page, null);
    }

    QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
    if (courseQuery.getTitle() != null) {
      wrapper.like("title", courseQuery.getTitle());
    }
    if (courseQuery.getTeacherId() != null) {
      wrapper.eq("teacher_id", courseQuery.getTeacherId());
    }
    if (courseQuery.getSubjectParentId() != null) {
      wrapper.eq("subject_parent_id", courseQuery.getSubjectParentId());
    }
    if (courseQuery.getSubjectId() != null) {
      wrapper.eq("subject_id", courseQuery.getSubjectId());
    }

    if (courseQuery.getStatus() != null) {
      wrapper.eq("status", courseQuery.getStatus());
    }

    return page(page, wrapper);
  }

  @Override
  public CoursePublishVo getCoursePublish(String id) {
    return baseMapper.getCoursePublish(id);
  }

  @Override
  public boolean publishCourse(String id, Boolean published) {
    EduCourse course = getById(id);
    if (published) {
      course.setStatus(CourseStatus.normal);// 设置为发布状态
    } else {
      course.setStatus(CourseStatus.draft);// 设置为发布状态
    }
    updateById(course);
    return true;
  }

  @Override
  public boolean removeCourse(String id) {
    eduVideoService.removeByCourseId(id);  // 删除小节及其视频
    eduChapterService.removeByCourseId(id);  // 删除章节
    eduCourseDescriptionService.removeById(id);  // 删除课程描述
    return removeById(id);  // 删除课程
  }
}
